// action的统一管理

import axios from 'axios';

export const changeInputAction = value => ({
    type: 'change_input',
    value,
});

export const addItemAction = () => ({
    type: 'add_item',
});

export const delItemAction = index => ({
    type: 'del_item',
    index,
});

export const initListAction = list => ({
    type: 'init_list',
    list,
});

// 使用redux-thunk后，action可以返回函数，用于进行异步请求(axios)
export const getTodoList = () => {
    return dispatch => {
        let list = [];
        axios.get('https://www.fastmock.site/mock/0764b93cba70add273910b232c51aad8/development/api/getHotList').then(function (res) {
            if (res.data.data.length > 0) {
                for (const val of res.data.data) {
                    list.push(val.name);
                }
            }
            const action = initListAction(list);
            dispatch(action); // 将action传给store
        });
    };
};
